// pages/results/results.js
import { myGetRequest,myPostRequest } from "../../utils/myRequest.js"
var app = getApp();
Page({
  data: {
    totalScore: null, // 分数
    wrongList: [], // 错误的题数-乱序
    wrongListSort: [],  // 错误的题数-正序
    chooseValue: [], // 选择的答案
    remark: ["好极了！你很棒棒哦","哎哟不错哦","别灰心，继续努力哦！"], // 评语
    modalShow: false
  },
  onLoad: function (options) {
    console.log(options);
    // wx.setNavigationBarTitle({ title: options.testId }) // 动态设置导航条标题
    
    let wrongList = JSON.parse(options.wrongList);
    let wrongListSort = JSON.parse(options.wrongListSort);
    let chooseValue = JSON.parse(options.chooseValue);
    let questionList = JSON.parse(options.questionList);
    this.setData({
      totalScore: options.totalScore != ""?options.totalScore:"无",
      wrongList: wrongList,
      wrongListSort: wrongListSort,
      chooseValue: chooseValue,
      questionList: questionList,
      // questionList: app.globalData.questionList[options.testId],  // 拿到答题数据
      // testId: options.testId  // 课程ID
    })
    console.log(this.data.chooseValue);
  },
  // 查看错题
  toView: function(){
    // 显示弹窗
    this.setData({
      modalShow: true
    })
  },
  
  //答题兑换健康能量
  getPower: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo')
    //  console.log(JSON.stringify(userInfo))

    wx.request({
      url: 'https://jianbuxing.qdszgh.cn/user/questionsforpower', // 服务器后端的地址
      data: {
        "id": userInfo.id,
        // 传给后端的数据
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },

      method: "POST", // 请求的方式有Get和Post两种
      success(res) {
        console.log(userInfo.id)
        if (res.statusCode == 200) {
          console.log(res.data.message)
          if (res.data.message == "notime") {
            wx.showToast({
              title: '已兑换过健康能量',
              icon: 'error',
            })
          } else {
            wx.showToast({
              title: '兑换成功',
              icon: 'suceess',
            })
          }

        } else {
          wx.showToast({
            title: '兑换失败',
            icon: 'error',
          })
        }
        // 这里写请求成功后会执行的代码
      }
    })
    
    
    wx.switchTab({
      url: '../index/index'
    })
  },

  // 返回首页
  toIndex: function(){
    wx.switchTab({
      url: '../index/index'
    })
  }
})
